<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>起搏器详情</title>
  <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
  <!-- <link rel="stylesheet" href="../../css/public.css" media="all"> -->
</head>

<body>
  <div class="layuimini-container">
    <div class="layuimini-main">

      <!-- <fieldset class="table-search-fieldset"> -->
      <div style="margin: 10px 10px 10px 10px">
        <div class="layui-form layui-form-pane" action="">
          <div class="layui-form-item">
            <div class="layui-form-item">
              <label class="layui-form-label">id号</label>
              <div class="layui-input-block">
                <input type="text" id="id" name="id" autocomplete="off" autocomplete="off"
                  class="layui-input" disabled>
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">起搏器名称</label>
              <div class="layui-input-block">
                <input type="text" id="name" name="name" autocomplete="off" autocomplete="off"
                  class="layui-input" lay-verify="name|required">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">型号</label>
              <div class="layui-input-block">
                <input type="text" id="modelNumber" name="modelNumber" autocomplete="off" autocomplete="off"
                  class="layui-input" lay-verify="modelNumber|required" >
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">序列号</label>
              <div class="layui-input-block">
                <input type="text" id="productNumber" name="productNumber" autocomplete="off" autocomplete="off"
                  class="layui-input" lay-verify="productNumber|required" >
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">公司</label>
              <div class="layui-input-block">
                <input type="text" id="companyName" name="companyName" autocomplete="off" autocomplete="off"
                  class="layui-input" lay-verify="companyName|required">
              </div>
            </div>
            <div style="position: absolute; right: 17px;">
              <button id="closebtn" type="button" class="layui-btn" style="margin-right: 0;" lay-submit lay-filter="saveBtn">保存提交</button>
            </div>
          </div>
        </div>
      </div>
      <!-- </fieldset> -->
    </div>
  </div>
  <script src="../../lib/layui-v2.6.3/layui.js"></script>
  <script src="../../js/basic-load.js" charset="utf-8"></script>
  <script>
    var method = "新增" // 也可能是"修改"，由传参决定
    layui.use(['laydate', 'form', 'jquery', 'table'], function () {
      var laydate = layui.laydate,
        form = layui.form,
        table = layui.table,
        $ = layui.$;

      //执行一个laydate实例
      laydate.render({
        elem: '#publication_data' //指定元素
      });
      form.verify({
        id:[
          /^(\d{0,20})?$/
          , '请输入正确的起搏器id，不超过20位'
        ]                
        , name: function (value) {
          if (value.length > 33) {
            return '起搏器名字最大长度不超过33个字符';
          }
        }
        , modelNumber: function (value) {
          if (value.length > 10) {
            return '起搏器型号最大长度不超过10个字符';
          }
        }
        , productNumber: function (value) {
          if (value.length > 10) {
            return '起搏器序列号最大长度不超过10个字符';
          }
        }
        , companyName: function (value) {
          if (value.length > 33) {
            return '公司名字最大长度不超过33个字符';
          }
        }
        ,
      });

      //监听提交
      form.on('submit(saveBtn)', function (data) {
        for (var key in data.field) {
          if (data.field[key] === '')
            delete data.field[key]
        }
        console.log(data.field)
        if(method == '新增'){
          delete data.field["id"]
        }
        console.log('修改用户信息，发送数据如下：')
        console.log(JSON.stringify(data.field))
        $.ajax({
          url: `${url_basic}/pacemaker/${method == '修改' ? 'update' : 'add'}`,
          type: "POST",
          data: JSON.stringify(data.field),
          contentType: 'application/json',
          success: function (data, textStatus) {
            console.log(data)
            console.log(textStatus)
            if (data.code == 0) {
              layer.msg('成功', { time: 0.5 * 1000 }, function () {
                // 关闭弹出层
                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
              });
              return true;
            }
            else {
              layer.msg('失败，原因：' + data.msg);
              return false;
            }
          },
          error: function (xhr, errorText, errorType) {
            console.log(xhr)
            console.log(xhr.status)
            console.log(errorType)
            layer.msg('修改失败（与服务器连接失败）');
          },
          complete: function () {
            console.log('---------')
          },
          dataType: "json"
        })
        return false;
      });
    });

    function setData(data, privilege,_method) {
      this_privilege = privilege
      console.log(data)
      if(_method == 1 ){
        method = '新增'
        document.getElementById('id').value = '注册后分配'
      }else{
        method = '修改'
        document.getElementById('id').value = data.id
        document.getElementById('name').value = data.name
        document.getElementById('modelNumber').value = data.modelNumber
        document.getElementById('productNumber').value = data.productNumber
        document.getElementById('companyName').value = data.companyName
      }

    };
    function resetPassword(){
      console.log("turn to visable")
      document.getElementById('passwordBlock').style.visibility = "visible"
    }
  </script>
</body>

</html>